#coding=utf-8
#Boa:Dialog:DlgCustomerChoice

import wx

from product import *
from MyListCtrl import *
from config import *

import DlgCustomerEditor

def create(parent):
    return DlgCustomerChoice(parent)

[wxID_DLGCUSTOMERCHOICE, wxID_DLGCUSTOMERCHOICEBTNADD, 
 wxID_DLGCUSTOMERCHOICEBTNCANCEL, wxID_DLGCUSTOMERCHOICEBTNMODIFY, 
 wxID_DLGCUSTOMERCHOICEBTNREFRESH, 
] = [wx.NewId() for _init_ctrls in range(5)]

class DlgCustomerChoice(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DLGCUSTOMERCHOICE,
              name=u'DlgCustomerChoice', parent=prnt, pos=wx.Point(212, 165),
              size=wx.Size(941, 589), style=0,
              title=u'\u5ba2\u6237\u9009\u62e9')
        self.SetClientSize(wx.Size(933, 555))
        self.Bind(wx.EVT_ACTIVATE, self.OnDlgCustomerChoiceActivate)

        self.btnAdd = wx.Button(id=wxID_DLGCUSTOMERCHOICEBTNADD,
              label=u'\u65b0\u589e\u5ba2\u6237', name=u'btnAdd', parent=self,
              pos=wx.Point(0, 0), size=wx.Size(75, 24), style=0)
        self.btnAdd.Bind(wx.EVT_BUTTON, self.OnBtnAddButton,
              id=wxID_DLGCUSTOMERCHOICEBTNADD)

        self.btnModify = wx.Button(id=wxID_DLGCUSTOMERCHOICEBTNMODIFY,
              label=u'\u4fee\u6539', name=u'btnModify', parent=self,
              pos=wx.Point(80, 0), size=wx.Size(75, 24), style=0)
        self.btnModify.Bind(wx.EVT_BUTTON, self.OnBtnModifyButton,
              id=wxID_DLGCUSTOMERCHOICEBTNMODIFY)

        self.btnCancel = wx.Button(id=wxID_DLGCUSTOMERCHOICEBTNCANCEL,
              label=u'\u9000\u51fa', name=u'btnCancel', parent=self,
              pos=wx.Point(700, 0), size=wx.Size(75, 24), style=0)
        self.btnCancel.Bind(wx.EVT_BUTTON, self.OnBtnCancelButton,
              id=wxID_DLGCUSTOMERCHOICEBTNCANCEL)

        self.btnRefresh = wx.Button(id=wxID_DLGCUSTOMERCHOICEBTNREFRESH,
              label=u'\u663e\u793a\u5168\u90e8', name=u'btnRefresh',
              parent=self, pos=wx.Point(616, 0), size=wx.Size(75, 24), style=0)
        self.btnRefresh.Bind(wx.EVT_BUTTON, self.OnBtnRefreshButton,
              id=wxID_DLGCUSTOMERCHOICEBTNREFRESH)

    def __init__(self, parent):
        self._init_ctrls(parent)

        self.myInit()
        
    def myInit(self):
        self.SetSize(wx.Size(800, 400))
        
##        self.listCtrl = MyListCtrl(parent=self, cols=Cfg.getCustPopupColsMode(),
##                                    clsDataModel=CDataCustomer)
        self.listCtrl = MyCustomerListCtrl(self)
        self.listCtrl.SetSize(wx.Size(800, 375))
        self.listCtrl.Move(wx.Point(0, 25))
        
        # 141208 - 影响界面显示效率，改为增加"刷新"按钮
        #self.listCtrl.refresh()
        
        self.listCtrl.Bind(wx.EVT_LEFT_DCLICK, self.OnSelectCustomer)

##        self.btnAdd.Move(wx.Point(35, 375))
##        self.btnModify.Move(wx.Point(120, 375))
##        self.btnOK.Move(wx.Point(240, 375))
##        self.btnCancel.Move(wx.Point(325, 375))
        
    def setFilter(self, str):
        self.listCtrl.refresh({'all':str})

    def Dismiss(self):
        print 'Dismiss'
        self.Hide()
    
    def Popup(self, pos):
        print 'Popup'
        self.Move(pos)
        self.Show()
        
    def Show(self, show=True):
        print 'show %d' %show
        super(DlgCustomerChoice, self).Show(show)
        print 'shown %d' %self.IsShown()
        
    def OnSelectCustomer(self, event):
        print 'OnSelectCustomer'
        
        item = self.listCtrl.GetNextItem(-1, state = wx.LIST_STATE_SELECTED)
        if (item == -1):
            return
        
        #self.Show(False)
        #self.Close()
        
        fields = {'id':self.listCtrl.GetItemText(item)}
        #self.GetParent().setCustomer(CDataCustomer.getData(fields))
        self.GetParent().setSelectPeer(CDataCustomer.getData(fields))
        #self.Destroy()
        #self.Dismiss()
        self.Show(False)
        #print 'setCustomer finished'

##    def OnDlgCustomerChoiceKillFocus(self, event):
##        event.Skip()
##        print 'OnDlgCustomerChoiceKillFocus'
##        self.Show(False)
##
##    def OnDlgCustomerChoiceActivate(self, event):
##        print 'OnDlgCustomerChoiceActivate'
##        event.Skip()
##        
##        #self.Show(event.GetActive())

    def OnDlgCustomerChoiceActivate(self, event):
        ''''''
        print 'OnDlgCustomerChoiceActivate %d' %event.GetActive()
        print 'self active %d' %self.IsActive()
        #event.Skip()
        if event.GetActive():
            self.Show()

    def OnBtnAddButton(self, event):
        dlg = DlgCustomerEditor.create(kit.getRootWin(self))
        if (dlg.ShowModal() == wx.ID_OK):
            self.GetParent().setSelectPeer(dlg.curObj)
            self.listCtrl.refresh()
            self.Show(False)

    def OnBtnCancelButton(self, event):
        print 'OnBtnCancelButton'
        #点击退出时，理应不修改parent中的customer，此处当时为何加了None??
        #self.GetParent().setCustomer(None)
        self.Show(False)
        #self.EndModal(wx.ID_CANCEL)

    def OnBtnModifyButton(self, event):
        '''修改选中的客户信息'''
        item = self.listCtrl.GetNextItem(-1, state = wx.LIST_STATE_SELECTED)
        if (item == -1):
            dlg = wx.MessageDialog(self, u'请选择列表项目', u'提示',
                                    wx.OK|wx.ICON_ERROR)
            dlg.ShowModal()
            return
        
        val = int(self.listCtrl.GetItem(item).GetText())

        custObj = CDataCustomer.getFirstInstance({CDataCustomer.COL_ID:val})
        if (custObj is None):
            dlg = wx.MessageDialog(self, u'未找到客户', u'提示',
                                    wx.OK|wx.ICON_ERROR)
            dlg.ShowModal()
            return
        
        dlg = DlgCustomerEditor.create(self)
        dlg.setDataObj(custObj)
        if (dlg.ShowModal() == wx.ID_OK):
            self.listCtrl.refresh()

    def OnBtnRefreshButton(self, event):
        self.listCtrl.refresh()

